package com.itheima.consumer.listener;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.Map;

@Slf4j
@Component
public class SpringRabbitListener {
//    @RabbitListener(queues = "simple.queue")
//    public void listenSimpleQueueMessage(String msg) throws InterruptedException {
//        System.out.println("spring 消费者接受到消息：【" + msg + "】");
//    }

    /**
     * 工作队列模式-消费者1
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(100);
    }

    /**
     * 工作队列模式-消费者2
     * @param msg
     * @throws InterruptedException
     */
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(50);
    }

    /**
     *广播模式-消费者1
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }
    /**
     *广播模式-消费者2
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }
    /**
     *广播模式-消费者3
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue3(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }
    /**
     *广播模式-消费者4
     */
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue4(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }

    /**
     *路由模式-消费者1
     */
    @RabbitListener(queues = "direct.queue1")
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1接收到Fanout消息：【" + msg + "】");
    }
    /**
     *路由模式-消费者2
     */
    @RabbitListener(queues = "direct.queue2")
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2接收到Fanout消息：【" + msg + "】");
    }

    /**
     * 主题模式-消费者1
     * @param msg
     */
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "hmall.topic",type = ExchangeTypes.TOPIC),
            key = {"red","blue"}
    ))
    public void listenTopicQueue1(String msg){
        System.out.println("消费者1接收到的消息：【" + msg + "】");
    }

    /**
     * 主题模式-消费者2
     * @param msg
     */
    @RabbitListener(bindings =@QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "hmall.topic",type = ExchangeTypes.TOPIC),
            key = {"red","yellow"}
    ))
    public void listenTopicQueue2(String msg){
        System.out.println("消费者2接收到的消息：【" + msg + "】");
    }



    @RabbitListener(queues = "object.queue")
    public void listenSimpleQueueMessage(Map<String, Object> msg) throws InterruptedException {
        System.out.println("消费者接收到object.queue消息：【" + msg + "】");
    }

    /**
     * 惰性队列
     * @param msg
     */
    //掌握注解方式
    @RabbitListener(queuesToDeclare = @Queue(
            name = "lazy.queue",
            durable = "true",
            arguments = @Argument(name = "x-queue-mode", value = "lazy")
    ))
    public void listenLazyQueue(String msg){
        log.info("接收到 lazy.queue的消息：{}", msg);
    }
}
